import Vue from 'vue';
import Router from 'vue-router';
import libs from '@/assets/js/libs';
import {
  changeTitle,
  getToken,
  showLogin
} from '@/assets/js/hscfApi';

import Layout from './views/layout.vue';

Vue.use(Router);

const router = new Router({
  mode: process.env.NODE_ENV !== 'development' ? 'history' : 'hash',
  routes: [{
    path: '/',
    component: Layout,
    children: [{
        path: '',
        name: 'index',
        meta: {
          title: '首页',
        },
        component: (resolve) => {
          require(['./views/index.vue'], resolve);
        }
      },
      {
        path: '/login',
        name: 'login',
        meta: {
          title: '发现',
        },
        component: (resolve) => {
          require(['./views/login.vue'], resolve);
        }
      },
      {
        path: '/mine',
        name: 'mine',
        meta: {
          title: '我的',
        },
        component: (resolve) => {
          require(['./views/mine.vue'], resolve);
        }
      },
      {
        path: '/info',
        name: 'info',
        meta: {
          title: '身份认证',
        },
        component: (resolve) => {
          require(['./views/info.vue'], resolve);
        }
      },
      {
        path: '/update',
        name: 'update',
        meta: {
          title: '身份认证',
        },
        component: (resolve) => {
          require(['./views/update.vue'], resolve);
        }
      },
      {
        path: '/list',
        name: 'list',
        meta: {
          title: '发现',
        },
        component: (resolve) => {
          require(['./views/list.vue'], resolve);
        }
      },
      {
        path: '/detail/:id',
        name: 'detail',
        meta: {
          title: '发现',
        },
        component: (resolve) => {
          require(['./views/detail.vue'], resolve);
        }
      },
      {
        path: '/news',
        name: 'news',
        meta: {
          title: '咨询',
        },
        component: (resolve) => {
          require(['./views/news.vue'], resolve);
        }
      },
      {
        path: '/information',
        name: 'information',
        meta: {
          title: '咨询',
        },
        component: (resolve) => {
          require(['./views/information.vue'], resolve);
        }
      },
      {
        path: '/erwm',
        name: 'erwm',
        meta: {
          title: '咨询',
        },
        component: (resolve) => {
          require(['./views/erwm.vue'], resolve);
        }
      },
      {
        path: '/task',
        name: 'task',
        meta: {
          title: '成功率',
        },
        component: (resolve) => {
          require(['./views/task.vue'], resolve);
        }
      },

    ]
  }, ]
});

router.beforeEach((to, from, next) => {
  if (to.meta.auth && !getToken()) {
      showLogin();
      return;
  }

  if (to.meta.title) {
    document.title = to.meta.title;
    changeTitle(to.meta.title)
  }

  // 不是惠商车服app跳转到活动介绍页
//if (to.name !== 'detail' && to.name !== 'invite' && !libs.isHSCFApp()) {
//  router.push({
//    name: 'detail',
//  });
//  return;
//}

  next();
});

window.$router = router;
export default router;
